package com.managermype.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.managermype.dao.interfaces.EnterpriseDao;
import com.managermype.domain.Enterprise;

@SuppressWarnings({ "unchecked" })
@Repository
public class EnterpriseDaoHibernateImpl implements EnterpriseDao {

	@Resource
	private SessionFactory sessionFactory;

	@Override
	public List<Enterprise> listByName(String name) throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		List<Enterprise> lstEnterprises;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from Enterprise where upper(name) like :name");
			query.setParameter("name", "%"+name.toUpperCase()+"%");
			lstEnterprises = query.list();
			tx.commit();
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			throw e;
		}
		finally {
			session.close();
		}
		return lstEnterprises;
	}

	@Override
	public List<Enterprise> listAll() throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		List<Enterprise> enterprises;
		try {
			tx = session.beginTransaction();
			enterprises = session.createQuery("from Enterprise").list();
			tx.commit();
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			throw e;
		}
		finally {
			session.close();
		}
		return enterprises;
	}

	@Override
	public Enterprise getByRuc(String ruc) throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		List<Enterprise> lstEnterprises;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from Enterprise where ruc = :ruc");
			query.setParameter("ruc", ruc);
			lstEnterprises = query.list();
			tx.commit();
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			throw e;
		}
		finally {
			session.close();
		}
		return lstEnterprises.size() > 0 ? (Enterprise)lstEnterprises.get(0):null;
	}

	@Override
	public Enterprise getById(Integer idEnterprise) throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		Enterprise enterprise;

		try {
			tx = session.beginTransaction();
			enterprise = (Enterprise)session.get(Enterprise.class, idEnterprise);
			tx.commit();
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			throw e;
		}
		finally {
			session.close();
		}

		return enterprise;
	}

	@Override
	public Integer countAllRegistred() throws HibernateException {
		return 0;
	}

	@Override
	public Enterprise insert(Enterprise enterprise) throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		Integer idEnterprise;

		try {
			tx = session.beginTransaction();
			idEnterprise = (Integer) session.save(enterprise);
			enterprise.setIdEnterprise(idEnterprise);
			tx.commit();
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			throw e;
		}
		finally {
			session.close();
		}

		return enterprise;
	}

	@Override
	public void update(Enterprise enterprise) throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(enterprise);
			tx.commit();			
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			throw e;
		}finally{
			session.close();
		}
		
	}

}
